<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="14">
        <el-col :span="8">
          <el-input placeholder="请输入商品名称" v-model="queryInfo.query" class="input-with-select" size="mini" clearable @clear='getGoodsList'>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="mini" @click="addgoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品展示表格 -->
      <el-table :data="goodsList" border stripe size="mini">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click=removeGoodsById(scope.row.goods_id)>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.goodsTotal">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'goods',
  data() {
    return {
      queryInfo: { query: '', pagenum: 1, pagesize: 10 },
      goodsList: [],
      goodsTotal: 0,
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品数据列表获取失败！')
      }
      for (let item of res.data.goods) {
        item.add_time = this.formatTime(item.add_time)
      }
      this.goodsList = res.data.goods
      this.goodsTotal = res.data.total
      // console.log(this.goodsList, this.goodsTotal)
    },
    //把时间戳转换为我们能识别的时间方法
    formatTime(time) {
      var date = new Date(time)
      const year = date.getFullYear()
      const mouth = (date.getMonth() + 1).toString().padStart(2, '0') //padStart(2,'0')取两位不够补0
      const day = date.getDate().toString().padStart(2, '0')
      const hours = date.getHours().toString().padStart(2, '0')
      const minute = date.getMinutes().toString().padStart(2, '0')
      return `${year}-${mouth}-${day}/${hours}:${minute}`
    },
    handleSizeChange(pagesize){
      this.queryInfo.pagesize = pagesize
      this.getGoodsList()
    },
    handleCurrentChange(pagenum){
      this.queryInfo.pagenum = pagenum
      this.getGoodsList()
    },
    // serchClose(){
    //   this.queryInfo = { query: '', pagenum: 1, pagesize: 10 }
    //   this.getGoodsList()
    // }

    // 删除商品
    //
    removeGoodsById(id){
      this.$confirm('确定删除该商品么？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await this.$http.delete(`goods/${id}`)
          // console.log(id)
          if(res.meta.status !==200){
            return this.$message.error('删除商品失败！')
          }
          this.$message.success('商品删除成功！')
          this.getGoodsList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },

    //编程式导航
    addgoods(){
      this.$router.push('/goods/addgoods')
    }
  },
}
</script>

<style lang="scss" scoped>
</style>